#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

int fun(int n)
{
    int count = 0;
    while (n)
    {
        if (n % 2 == 1)
            count++;
        n /= 2;
    }
    return count;
}
int main()
{
    int n = 0;
    while (scanf("%d", &n) != EOF)
    {
        if (n == 0)
            break;
        int i = 0;
        for (i = n + 1; 1; i++)
        {
            if (fun(i) == fun(n))
            {
                printf("%d\n", i);
                break;
            }
        }
    }

    return 0;
}